package com.miyu.system.service.log.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.Rediscache;
import com.miyu.system.dao.system.SysLogDao;
import com.miyu.system.pojo.system.SysLogSet;
import com.miyu.system.pojo.system.SysMenu;
import com.miyu.system.service.log.SysLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 系统日志服务Impl公司
 * @author miyu
 * @date 2022/12/28
 */
@Service
public class SysLogServiceImpl  implements SysLogService {

    @Resource
    private SysLogDao sysLogDao;
    @Override
    public JSONArray querySysLogSet(JSONObject param) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select set_id,method_name,pack_name from  sys_log_set ");
        sql.append("");
        return sysLogDao.queryJSONArrayBySql(sql.toString());
    }

    @Override
    public JSONObject saveSysLogSet(JSONObject param) {
        JSONObject rs = new JSONObject();
        SysLogSet sysLogSet = JSONObject.parseObject(param.toString(), SysLogSet.class);
        sysLogDao.saveOrUpdateByNotNullProperties(sysLogSet);
        rs.put("code","1");
        rs.put("msg","操作成功");
        return rs;
    }

    @Override
    public JSONObject delSysLogSet(JSONArray params) {
        JSONObject rs = new JSONObject();
        List<SysLogSet> sysLogSetList = params.toJavaList(SysLogSet.class);
        sysLogDao.deleteAll(sysLogSetList);
        rs.put("code","1");
        rs.put("msg","删除成功");
        return rs;
    }

    @Override
    public JSONObject syncSysLogSet() {
        JSONObject rs = new JSONObject();
        Set<String> methodName = new HashSet<>();
        Set<String> packName = new HashSet<>();
        List<SysLogSet> data = sysLogDao.findAll();
        for (SysLogSet s : data) {
            if (StringUtils.isNotBlank(s.getMethodName())){
                methodName.add(s.getMethodName());
            }
            if(StringUtils.isNotBlank(s.getPackName())){
                packName.add(s.getPackName());
            }
        }
        JSONObject syslog = new JSONObject();
        syslog.put("methodName",methodName);
        syslog.put("packName",packName);
        Rediscache.set("system:sysLog:sysLogSet",syslog);

        rs.put("code","1");
        rs.put("msg","同步成功");
        return rs;
    }
}
